<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Model\Admin\Article;
use App\Model\Admin\Links;
use App\Model\Admin\Category;
use App\Model\Home\Index;

class IndexController extends CommonController
{
  // 首页显示
	public function index(){
		// 文章推荐--显示所有文章
		$data = Article::orderBy('art_time','desc')->paginate(5);
		return view('home.index')->with('data',$data);
	}

	// 列表
	public function cate($cate_id){
		//查询所有文章，然后取当前分类以及所有子类的文章
		$article = Article::orderBy('art_time','desc')->get();
		$index = new Index;
		$sub = $index->getTree($article,$cate_id);
		$subnavs = Category::where('cate_pid',$cate_id)->get();
		// 
		$cate = Category::find($cate_id);

		//查看次数在增加
		Category::where('cate_id',$cate_id)->increment('cate_view');

		return view('home.list',compact('sub','cate','subnavs'));
	}

	// 文章详情页
	public function article($art_id){
		// 联合查询
		$article = Article::join('category','article.cate_id','=','category.cate_id')->where('art_id',$art_id)->first();

		//查看次数在增加
		Article::where('art_id',$art_id)->increment('art_view');

		// 查询上一级分类的信息
		$name = Category::where('cate_id',$article->cate_pid)->first();

		// 查询上一篇及下一篇
		$turnPage['pre'] = Article::where('art_id','<',$art_id)->orderBy('art_id','desc')->first();
		$turnPage['next'] = Article::where('art_id','>',$art_id)->orderBy('art_id','asc')->first();
		
		//查询相关文章
		$aboutArt = Article::where('cate_id',$article->cate_id)->orderBy('art_view','desc')->take(6)->get();
		return view('home.new')->with([
			'data'       =>   $article,
			'cate'       =>   $name,
			'turnPage'   =>   $turnPage,
			'aboutArt'   =>   $aboutArt,
			]);
	}
}
